<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" v-auth="'order.create'" @click="create"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" v-auth="'order.delete'" v-if="selection.length>0" :disabled="selection.length==0" @click="dels"></el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search" placeholder="搜索" @clear="this.$TABLE.searchSubmit(this)" clearable></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="this.$TABLE.searchSubmit(this)"></el-button>
                    <imiFilterBar :options="options" @filterChange="filterChange"></imiFilterBar>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" :params="params" @selection-change="selectionChange" @cell-dblclick="doubleClick" stripe remoteSort remoteFilter>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="id" prop="id" >
                                    
				</el-table-column>
                <el-table-column label="订单类型" prop="type" >
                    <template #default="scope">
                        <el-tag :type="scope.row.type == 'goods' ? 'success' : 'info'">{{type_option[scope.row.type]}}</el-tag>
                    </template>      
				</el-table-column>

                <el-table-column label="店铺" prop="" >
                    <template #default="scope"  >
                    <el-tag >{{store_id_option[scope.row.store_id]}} </el-tag>
                    </template>   
				</el-table-column>


                <el-table-column label="订单号" prop="order_sn" >
                                    
				</el-table-column>
                <el-table-column label="用户" prop="user_id" >
                                    
				</el-table-column>
                <el-table-column label="商品原价" prop="goods_original_amount" >
                                    
                </el-table-column>

                <el-table-column label="总运费" prop="dispatch_amount" >
                                    
                </el-table-column>

                <el-table-column label="运费优惠" prop="dispatch_discount_money" >
                                    
                </el-table-column>

                <el-table-column label="活动优惠" prop="activity_discount_money" >
                                    
                    </el-table-column>



                <el-table-column label="商品总价" prop="goods_amount" >
                                    
				</el-table-column>



                <el-table-column label="订单总金额" prop="total_amount" >
                                    
                </el-table-column>
                <el-table-column label="积分总数" prop="score_amount" >
                                    
                </el-table-column>
                <el-table-column label="支付金额" prop="total_fee" >
                                    
                </el-table-column>
                <!-- <el-table-column label="改价前金额" prop="last_total_fee" >
                                    
                </el-table-column> -->
                <!-- <el-table-column label="折扣总金额" prop="discount_fee" >
                                    
                </el-table-column> -->
                <el-table-column label="平台优惠券抵用金额" prop="coupon_fee" >
                                    
                </el-table-column>
                <el-table-column label="店铺优惠券抵用金额" prop="store_coupon_fee" >
                                    
                </el-table-column>
 
                <el-table-column label="实际支付金额" prop="pay_fee" >
                                    
                </el-table-column>
                <el-table-column label="支付贝壳总数" prop="shell_fee" >
                                    
                </el-table-column>
                <el-table-column label="积分支付数" prop="score_fee" >
                                    
                </el-table-column>
                <el-table-column label="积分服务费金额" prop="score_money_fee" >
                                    
                </el-table-column>


                
       
                <el-table-column label="联系电话" prop="phone" >
                                    
				</el-table-column>
                <el-table-column label="收货人" prop="consignee" >
                                    
				</el-table-column>
                <el-table-column label="省" prop="province_name" >
                                    
				</el-table-column>
                <el-table-column label="市" prop="city_name" >
                                    
				</el-table-column>
                <el-table-column label="区" prop="area_name" >
                                    
				</el-table-column>
                <el-table-column label="乡镇" prop="town_name" >
                                    
				</el-table-column>
                <el-table-column label="详细地址" prop="address" >
                                    
				</el-table-column>
                <!-- <el-table-column label="省ID" prop="province_id" >
                                    
				</el-table-column>
                <el-table-column label="市ID" prop="city_id" >
                                    
				</el-table-column>
                <el-table-column label="区ID" prop="area_id" >
                                    
				</el-table-column>
                <el-table-column label="镇ID" prop="town_id" >
                                    
				</el-table-column> -->
                <el-table-column label="订单状态" prop="status" >
                    <template #default="scope">
                        <el-tag :type="scope.row.status == '1' ? 'success' : 'info'">{{status_option[scope.row.status]}}</el-tag>
                    </template>  
				</el-table-column>
                <el-table-column label="发票开具状态" prop="invoice_status" >
                    <template #default="scope">
                        <el-tag :type="scope.row.invoice_status == '1' ? 'success' : 'info'">{{invoice_status_option[scope.row.invoice_status]}}</el-tag>
                    </template>         
				</el-table-column>
                <el-table-column label="商户备注" prop="memo" >
                                    
				</el-table-column>
                <el-table-column label="用户备注" prop="remark" >
                                    
				</el-table-column>

 
                <el-table-column label="优惠券 id" prop="coupons_id" >
                                    
				</el-table-column>
                <el-table-column label="交易单号" prop="transaction_id" >
                                    
				</el-table-column>
                <el-table-column label="交易原始数据" prop="payment_json" >
                                    
				</el-table-column>
                <el-table-column label="支付方式" prop="pay_type" >
                    <template #default="scope">
                        <el-tag :type="scope.row.pay_type == '1' ? 'success' : 'info'">{{pay_type_option[scope.row.pay_type]}}</el-tag>
                    </template>    
				</el-table-column>
                <el-table-column label="支付时间" prop="paytime"  align="right" width="180" :formatter="this.$TABLE.datetime">
                                    
				</el-table-column>
                <el-table-column label="附加字段" prop="ext" >
                                    
				</el-table-column>
                <el-table-column label="平台" prop="platform" >
                                    
				</el-table-column>
                <el-table-column label="创建时间" prop="createtime"  align="right" width="180" :formatter="this.$TABLE.datetime">
                                    
				</el-table-column>
                <el-table-column label="更新时间" prop="updatetime"  align="right" width="180" :formatter="this.$TABLE.datetime">
                                    
				</el-table-column>
                <el-table-column label="删除时间" prop="deletetime"  align="right" width="180" :formatter="this.$TABLE.datetime">
                                    
				</el-table-column>
          

                <!-- <el-table-column label="活动类型" prop="activity_type" >
                    <template #default="scope" v-if="activity_type">
                        <el-tag :type="scope.row.activity_type == '1' ? 'success' : 'info'">{{activity_type_option[scope.row.activity_type]}}</el-tag>
                    </template>  
				</el-table-column> -->
                
                <el-table-column label="操作" fixed="right" align="right" width="200">
                    <template #default="scope">
                      <el-button type="text" v-auth="'order.update'" size="small" @click="checkSubOrder(scope.row)">查看
                      </el-button>
                        <el-button type="text" v-auth="'order.update'" size="small" @click="update(scope.row)">编辑
                        </el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="del(scope.row)">
                            <template #reference>
                                <el-button type="text" v-auth="'order.delete'" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>
<script>
    import saveDialog from './save';
    import imiFilterBar from '@/components/imiFilterBar';
    export default {
        name: 'order',
        components: {
            saveDialog,
            imiFilterBar
        },

        data() {
            return {
                store_id_option:[],
                options: [
                    {
                        label: 'id',
                        value: 'id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '订单类型',
                        value: 'type',
                        type: 'select',
                        operator: '=',
                    	extend: {
                            data: {"goods":"商城订单","score":"积分商城订单"}
	                    }
					},
					{
                        label: '订单号',
                        value: 'order_sn',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '用户',
                        value: 'user_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '活动类型',
                        value: 'activity_type',
                        type: 'switch',
                        operator: '=',
                    	extend: {
                            data: {"0":"关闭","1":"开启"}
	                    }
					},
					{
                        label: '商品总价',
                        value: 'goods_amount',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '总运费',
                        value: 'dispatch_amount',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '联系电话',
                        value: 'phone',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '收货人',
                        value: 'consignee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '省',
                        value: 'province_name',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '市',
                        value: 'city_name',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '区',
                        value: 'area_name',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '乡镇',
                        value: 'town_name',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '详细地址',
                        value: 'address',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '省ID',
                        value: 'province_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '市ID',
                        value: 'city_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '区ID',
                        value: 'area_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '镇ID',
                        value: 'town_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '订单状态',
                        value: 'status',
                        type: 'select',
                        operator: '=',
                    	extend: {
                            data: [{label:"开启",value:1},{label:"关闭",value:0}]
	                    }
					},
					{
                        label: '发票开具状态',
                        value: 'invoice_status',
                        type: 'select',
                        operator: '=',
                    	extend: {
                            data: {"-1":"不可开具","0":"未申请","1":"已申请"}
	                    }
					},
					{
                        label: '商户备注',
                        value: 'memo',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '用户备注',
                        value: 'remark',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '订单总金额',
                        value: 'total_amount',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '积分总数',
                        value: 'score_amount',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '支付金额',
                        value: 'total_fee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '改价前金额',
                        value: 'last_total_fee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '折扣总金额',
                        value: 'discount_fee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '平台优惠券抵用金额',
                        value: 'coupon_fee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '店铺优惠券抵用金额',
                        value: 'store_coupon_fee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '活动优惠',
                        value: 'activity_discount_money',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '运费优惠',
                        value: 'dispatch_discount_money',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '实际支付金额',
                        value: 'pay_fee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '支付贝壳总数',
                        value: 'shell_fee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '积分支付数',
                        value: 'score_fee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '积分服务费金额',
                        value: 'score_money_fee',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '商品原价',
                        value: 'goods_original_amount',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '优惠券 id',
                        value: 'coupons_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '交易单号',
                        value: 'transaction_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '交易原始数据',
                        value: 'payment_json',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '支付方式',
                        value: 'pay_type',
                        type: 'select',
                        operator: '=',
                    	extend: {
                            data: {"wechat":"微信支付","alipay":"支付宝","wallet":"钱包支付","score":"积分支付","shell":"贝壳支付","mix":"混合支付"}
	                    }
					},
					{
                        label: '支付时间',
                        value: 'paytime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '附加字段',
                        value: 'ext',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '平台',
                        value: 'platform',
                        type: 'select',
                        operator: '=',
                    	extend: {
                            data: {"H5":"H5","wxOfficialAccount":"微信公众号","wxMiniProgram":"微信小程序","App":"App"}
	                    }
					},
					{
                        label: '创建时间',
                        value: 'createtime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '更新时间',
                        value: 'updatetime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '删除时间',
                        value: 'deletetime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '店铺ID',
                        value: 'store_id',
                        type: 'text',
                        operator: '=',
                    },
					                    
                ],
                dialog: {
                    save: false
                },
                apiObj: this.$API.order.read,
                selection: [],
                search: '',
                params:{},

                type_option:{"goods":"商城订单","score":"积分商城订单"},
				activity_type_option:{"0":"关闭","1":"开启"},
				status_option:{"-2":"交易关闭","-1":"已取消","0":"未支付","1":"已支付","2":"已完成"},
				invoice_status_option:{"-1":"不可开具","0":"未申请","1":"已申请"},
				pay_type_option:{"wechat":"微信支付","alipay":"支付宝","wallet":"钱包支付","score":"积分支付","shell":"贝壳支付","mix":"混合支付"},
				platform_option:{"H5":"H5","wxOfficialAccount":"微信公众号","wxMiniProgram":"微信小程序","App":"App"},
            }
        },
        created() {
            this.$TABLE.setFilterValue(this);
        },
        mounted() {
            this.getStore();//表格加载时会执行该方法
        },
        methods: {
            async getStore(){//需要加入的方法
                
                var res = await this.$API.store.read.get();
                var list = res.data.list;
                var store_list_option = [];
                for (let index = 0; index < list.length; index++) {
                    store_list_option[list[index].id] = list[index].name;                    
                }
                console.log(store_list_option);
                this.store_id_option = store_list_option;
            },
            doubleClick(row) {
                this.update(row);
            },
            filterChange(data) {
                this.$TABLE.filter(data, this);
            },
            create() {
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open()
                })
            },
            update(row) {
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open('update').setData(row)
                })
            },
            // 点击查看子订单
            checkSubOrder(row){
              let id = JSON.parse(JSON.stringify(row)).id
              this.$router.push({
                path: "/order_item/index",
                query: {
                  id: id
                },
              });
            },
            async del(row) {
                var res = await this.$API.order.delete.post({ids: row.id});
                if (res.code == 200) {
                    this.$refs.table.refresh()
                    this.$message.success(res.message)
                } else {
                    this.$alert(res.message, "提示", {type: 'error'})
                }
            },
            async dels() {
                if (!this.selection.length) {
                    this.$message.warning("请选择要删除的数据")
                    return false;
                }
                var ids = [];
                for (const i in this.selection) {
                    ids.push(this.selection[i].id)
                }
                this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                    type: 'warning'
                }).then(() => {
                    const loading = this.$loading();
                    this.$API.order.delete.post({ids:ids}).then(res => {
                        if (res.code == 200) {
                            this.$refs.table.refresh()
                            this.$message.success(res.message)
                        } else {
                            this.$message.error(res.message)
                        }
                        loading.close();
                    });
                })
            },
            selectionChange(selection) {
                this.selection = selection;
            },
            handleSuccess() {
                this.$refs.table.refresh()
            }
        }
    }
</script>